<?php
namespace V1\Model;
use Think\Model;
/**
 * 碎片表操作
 */
class TagModel extends Model {
    /**
    * 数据表前缀
    * @var string
    */
   protected $tablePrefix = MTCORE_TABLE_PREFIX;
   /**
    * 数据库连接
    * @var string
    */
   protected $connection = MTCORE_DB_DSN;
    /**
     * 获取马蹄指数信息
     * @return array 列表
     */
    public function queryList($where = array(), $order = 'C1D desc'){
       $querysql="SELECT stockcode,stockname," .
       		"SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 2 HOUR) >= NOW() THEN newscount ELSE 0 END ) AS C2H " .
       		",SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 24 HOUR) >= NOW() THEN newscount ELSE 0 END) AS C1D" .
       		",SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 2 DAY) >= NOW() THEN newscount ELSE 0 END ) AS C2D" .
       		",SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 7 DAY) >= NOW() THEN newscount ELSE 0 END) AS C1W" .
       		",SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 1 MONTH) >= NOW() THEN newscount ELSE 0 END) AS C1M" .
       		",SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 2 HOUR) >= NOW() THEN sentiment ELSE 0 END ) AS S2H" .
       		",SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 24 HOUR) >= NOW() THEN sentiment ELSE 0 END) AS S1D" .
       		",SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 2 DAY) >= NOW() THEN sentiment ELSE 0 END ) AS S2D" .
       		",SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 7 DAY) >= NOW() THEN sentiment ELSE 0 END) AS S1W" .
       		",SUM( CASE WHEN DATE_ADD(publishdate,INTERVAL 1 MONTH) >= NOW() THEN sentiment ELSE 0 END) AS S1M  " .
       		"FROM stock_tag_hour as A  where 1=1 ";
       
        if(!empty($where['A.stathour'])) {
        	$querysql = str_replace("NOW()","'".$where['A.stathour']."'",$querysql);
        	$querysql .= " and publishdate<='".$where['A.stathour']."' ";
        }

	if(!empty($where['A.stocktype']) && $where['A.stocktype'] == 'HK') {
                $querysql.= " and  length(A.stockcode)=5  ";	
        }else {
        	$querysql.= " and  length(A.stockcode)=6  ";
        }
        
        $querysql.= " GROUP BY stockcode,stockname ORDER BY ".$order;
      
        $list= $this->query($querysql); 
        return $list;         
     
    }
    /**
     * 转行从数据库获取的list为页面上显示需要的newlist的值
     */
    public  function getNewList($list,$stathour,$sortkey="C1D"){
	    $newlist = array();
	    foreach ($list as $key=>$value)
		{
                    $record = $value;
                    $record_stockcode = $value['stockcode'];
                    $record_stockname = $value['stockname'];
                    $record_C2H = $value['c2h'];
                    $record_C1D = $value['c1d'];
                    $record_C2D = $value['c2d'];
                    $record_C1W = $value['c1w'];
                    $record_C1M = $value['c1m'];
                    $record_C = $record_C1D*1/2 + $record_C2D*1/6 +  $record_C1M*1/180;
                    $record_S2H = $record_C2H == 0 ? 0:$value['s2h']/$record_C2H;
                    $record_S1D = $record_C1D == 0 ? 0:$value['s1d']/$record_C1D;
                    $record_S2D = $record_C2D == 0 ? 0:$value['s2d']/$record_C2D;
                    $record_S1W = $record_C1W == 0 ? 0:$value['s1w']/$record_C1W;
                    $record_S1M = $record_C1M == 0 ? 0:$value['s1m']/$record_C1M;
                    $record_S =  $record_S1D*1/2 + $record_S2D*1/3  +  $record_S1M*1/6;
                    $record['publishdate'] = $stathour;
                    $record['C'] = number_format($record_C,4);
                    //$record['C'] = $record_C;
                    $record["S2H"] = number_format($record_S2H,4);
                    $record["S1D"] = number_format($record_S1D,4);
                    $record["S2D"] = number_format($record_S2D,4);
                    $record["S1W"] = number_format($record_S1W,4);
                    $record["S1M"] = number_format($record_S1M,4);
                    $record["S"] = number_format($record_S,4);
                    $newlist[$key]=$record;
		    //echo "key:".$key;
		    //var_dump($value);
		}

		$sort = array(
        	'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
        	'field'     => $sortkey,       //排序字段
		);
		$arrSort = array();
		foreach($newlist AS $uniqid => $row){
    		foreach($row AS $key=>$value){
        		$arrSort[$key][$uniqid] = $value;
    		}
		}
		if($sort['direction']){
    		array_multisort($arrSort[$sort['field']], constant($sort['direction']), $newlist);
		}
		
	    return $newlist;	    
    }
    
}
